package com.example.demo.controller;

import com.example.demo.entity.Sign;
import com.example.demo.exception.BusyException;
import com.example.demo.exception.sign.SignAllException;
import com.example.demo.exception.sign.SignExitException;
import com.example.demo.exception.sign.SignNotExitException;
import com.example.demo.service.SignService;
import com.example.demo.utils.Message;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("sign")
public class
SignController {
    @Resource
    SignService signService;

    /**
     * 新建标签
     * @param sign 标签  name标签名
     * @return
     */
    @RequestMapping(value="createSign",method = RequestMethod.POST)
    public Message createSign(@RequestBody Sign sign) {
        String msg = "新建标签成功";
        try {
            signService.createSign(sign);
            return Message.createSuc(msg);
        } catch (BusyException e) {
            e.printStackTrace();
            msg = "系统繁忙，请稍等";
            return Message.createErr(msg);
        } catch (SignExitException e) {
            e.printStackTrace();
            msg="该标签已存在，请勿重复标签";
            return Message.createErr(msg);
        }
    }

    /**
     * 为文件新建标签
     * @param sign  name标签名  path文件路径
     * @return
     */
    @RequestMapping(value="newSignFile",method = RequestMethod.POST)
    public Message newSignFile(@RequestBody Sign sign){
        String msg="文件标签新建成功";
        try{
            signService.newSignFile(sign);
            return Message.createSuc(msg);
        }catch(BusyException e){
            msg = "系统繁忙，请稍等";
            return Message.createErr(msg);
        }catch (SignExitException e) {
            e.printStackTrace();
            msg="该文件标签已存在，请勿重复标签";
            return Message.createErr(msg);
        }
    }

    /**
     * 删除文件标签
     * @param sign path name
     * @return
     */
    @RequestMapping(value="delSignFile",method = RequestMethod.DELETE)
    public Message delSignFile(@RequestBody Sign sign){
        String msg="文件标签删除成功";
        try{
            signService.delSignFile(sign);
            return Message.createSuc(msg);
        }catch(BusyException e){
            e.printStackTrace();
            msg = "系统繁忙，无法删除文件标签";
            return Message.createErr(msg);
        }catch (SignNotExitException e) {
            e.printStackTrace();
            msg="该文件标签不存在，无法删除";
            return Message.createErr(msg);
        }
    }

    /**
     * 覆盖原文件标签
     * @param newSign String[] 新的标签组
     * @param path 文件路径
     * @return
     */
    @RequestMapping(value="coverSign",method = RequestMethod.POST)
    public Message coverSign(@RequestBody String[] newSign,@RequestParam(value="path") String path){
        String msg="文件标签覆盖成功";
        try{
            signService.coverSign(newSign,path);
            return Message.createSuc(msg);
        }catch(BusyException e){
            e.printStackTrace();
            msg = "系统繁忙，无法覆盖文件标签";
            return Message.createErr(msg);
        }catch (SignNotExitException e) {
            e.printStackTrace();
            msg="该文件路径不存在，无法覆盖";
            return Message.createErr(msg);
        }
    }

    /**
     * 获取文件的标签，返回String数组
     * @param path 该文件的路径
     */
    @RequestMapping(value="getFileSign",method = RequestMethod.GET)
    public Message getFileSign(@RequestParam(value="path",required=false) String path){

        try {
            List<String> FileSign = signService.getFileSign(path);
            return Message.createSuc(FileSign);
        }catch (SignAllException e) {
            String msg;
            e.printStackTrace();
            msg="该文件标签不存在。";
            return Message.createErr(msg);
        }

    }

    /**
     * 获取所有标签
     * @return List<map> AllSign
     */
    @RequestMapping(value = "getAllSign",method = RequestMethod.GET)
    public Message getAllSign(){
        try{
            List<Map> AllSign=signService.getAllSign();
            return Message.createSuc(AllSign);
        }catch (SignAllException e) {
            String msg;
            e.printStackTrace();
            msg="该文件标签不存在。";
            return Message.createErr(msg);
        }
    }
}
